package com.jnshu.controller;

import com.jnshu.entity.Student;
import com.jnshu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
     private StudentService studentService;

    @RequestMapping("/login")
    public ModelAndView login(String sname, String password,
                              ModelAndView modelAndView, HttpSession session){
        //根据用户名和密码查找用户，判断用户登陆
        Student student=studentService.login(sname,password);
        if (student!=null){
            //登陆成功，将Student 对象设置到HttpSession 作用域
            session.setAttribute("student",student);
            modelAndView.setView(new RedirectView("stu/getAllUser"));

        }else {
            modelAndView.addObject("message", "登陆用户名或密码错误，请重新登陆");
            modelAndView.setViewName("loginForm");
        }
        return modelAndView;
    }

}
